package com.zhu.SupermarketInventoryManagement.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.SupermarketInventoryManagement.dao.IGoodsDao;
import com.zhu.SupermarketInventoryManagement.model.DO.GoodsDO;
import com.zhu.SupermarketInventoryManagement.model.QO.GoodsQO;
import com.zhu.SupermarketInventoryManagement.model.ResultCommon;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class GoodsService {

    @Resource
    IGoodsDao iGoodsDao;

    public ResultCommon list(GoodsQO goods) {
        Page<GoodsDO> page = new Page<>(goods.getPage(), goods.getLimit());
        IPage<GoodsDO> result = iGoodsDao.page(page);
        List<GoodsDO> records = result.getRecords();
        //result.getTotal()表示查询到的结果总数，result.getRecords()表示查询到的结果
        return ResultCommon.<List<GoodsDO>>builder().code(0).count(result.getTotal()).data(records).build();
    }

    public ResultCommon record(GoodsQO goods) {

        Page<GoodsDO> page = new Page<>(goods.getPage(), goods.getLimit());
        IPage<GoodsDO> result = iGoodsDao.page(page);
        List<GoodsDO> records = result.getRecords();
        //result.getTotal()表示查询到的结果总数，result.getRecords()表示查询到的结果
        return ResultCommon.<List<GoodsDO>>builder().code(0).count(result.getTotal()).data(records).build();
    }

    public ResultCommon<Object> update(GoodsQO goodsQO) {
        ResultCommon<Object> resultCommon = new ResultCommon<>();
        UpdateWrapper<GoodsDO> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", goodsQO.getId());
        GoodsDO goodsDO = GoodsDO.builder()
                .name(goodsQO.getName())
                .price(goodsQO.getPrice())
                .cost(goodsQO.getCost())
                .number(goodsQO.getNumber())
                .categoryId(goodsQO.getCategoryId())
                .build();
        iGoodsDao.update(goodsDO, wrapper);
//        System.out.println("商品修改成功\n内容为："+goodsDO);
        resultCommon.setCode(0);
//        System.out.println("GoodsService 状态码: "+resultCommon);
        return resultCommon;
    }

    public ResultCommon<Object> delete(String id) {
        ResultCommon<Object> resultCommon = new ResultCommon<>();
        iGoodsDao.removeById(id);
        return resultCommon;
    }

    public ResultCommon add(GoodsQO goodsQO) {
        //获取货物id
//        List<GoodsDO> list = iGoodsDao.list();
//        Integer id = 1;
//        if (list.size() > 0) {
//            for (GoodsDO goodsDO : list) {
//                id = goodsDO.getId() + 1;
//            }
//        }

        ResultCommon<Object> resultCommon = new ResultCommon<>();
        GoodsDO goodsDO = GoodsDO.builder()
//                .id(id)
                .name(goodsQO.getName())
                .price(goodsQO.getPrice())
                .cost(goodsQO.getCost())
                .number(goodsQO.getNumber())
                .categoryId(goodsQO.getCategoryId())
                .adminId(1)
                .build();
        try {
            iGoodsDao.save(goodsDO);
            resultCommon.setCode(0);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            resultCommon.setCode(500);
        }
        return resultCommon;
    }
}
